package com.fengxu.gamescript.boot

import android.animation.ValueAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.GradientDrawable
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.core.animation.doOnEnd
import androidx.core.view.isVisible
import com.fengxu.gamescript.R
import com.fengxu.gamescript.data.other.Point
import com.fengxu.gamescript.util.CommonUtils
import com.fengxu.gamescript.util.FloatUtils
import com.xuexiang.xui.utils.DensityUtils
import com.yhao.floatwindow.FloatWindow
import com.yhao.floatwindow.MoveType
import org.jetbrains.anko.sdk27.coroutines.onClick
import org.jetbrains.anko.textColor

/**
 * 键位设置引导类
 */
class KeySetBoot{

    companion object {
        // 引导类型的悬浮窗标签
        private const val BootFloatWinName = "float_boot_win"
        // 是否已经运行了一个引导
        var hasRunning = false
    }

    // 上下文对象
    private val mContext:Context

    // 引导悬浮窗组件
    private val mView:View

    // 显示实时移动位置的组件
    private val keyInfoTextView:TextView

    // 保存当前索引配置
    private val saveConfView: View

    // 窗口管理器
    private val windowManager:WindowManager

    // 需要引导的键位个数
    var keyBootNumber = 1

    // 背景色
    @ColorInt
    var bgColor:Int = Color.parseColor("#88FF0000")

    // 引导键大小
    var size = DensityUtils.dp2px(50F)

    // 字体大小
    var fontSize = DensityUtils.sp2px(8F)

    // 字体色
    @ColorInt
    var fontColor = Color.WHITE

    // 引导完成事件
    var keyBootComplete:((Array<Point>)->Unit)? = null

    constructor(ctx:Context){
        mContext = ctx
        mView = View.inflate(ctx, R.layout.flow_keyboot, null)
        val statementTextView = mView.findViewById<View>(R.id.flow_keyBoot_statement)
        mView.onClick { statementTextView.isVisible = !statementTextView.isVisible }
        keyInfoTextView = mView.findViewById(R.id.flow_keyBoot_keyInfo)
        saveConfView = mView.findViewById(R.id.flow_keyBoot_saveKeyInfo)
        windowManager = FloatUtils.getWindowManager(ctx.applicationContext)
    }


    /**
     * 显示引导界面
     */
    fun showBoot(){
        hasRunning = true
        FloatWindow.destroy(BootFloatWinName)
        FloatWindow.with(mContext.applicationContext)
            .setView(mView)
            .setWidth(DensityUtils.dp2px(300F))
            .setHeight(-2)
            .setMoveType(MoveType.active)
            .setDesktopShow(true)
            .setTag(BootFloatWinName)
            .build()
        initSmallIndexFloat()
        FloatWindow.get(BootFloatWinName).show()
    }

    /**
     * 初始化键位索引的小悬浮图标
     */
    @SuppressLint("SetTextI18n")
    private fun initSmallIndexFloat() {
        val screenSize = CommonUtils.getScreenMetric()
        // 存储所有当前索引的位置
        val views = Array<View?>(keyBootNumber){ null }
        for (i in keyBootNumber downTo 1){
            val lp = FloatUtils.getDefaultFloatParams()
            lp.flags = lp.flags or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            lp.width = size;  lp.height = size;
            lp.x = 0;   lp.y = (screenSize.widthPixels * 0.7).toInt()
            // 初始化索引悬浮窗
            val bootTextView = TextView(mContext).apply {
                text = i.toString()
                textSize = fontSize.toFloat()
                textColor = fontColor
                gravity = Gravity.CENTER
                background = GradientDrawable().apply {
                    setStroke(1, Color.TRANSPARENT)
                    cornerRadius = (size / 2).toFloat()
                    this.setColor(bgColor)
                }
                bringToFront()
            }
            // 添加到系统桌面
            windowManager.addView(bootTextView, lp)
            views[i - 1] = bootTextView
            // 设置悬浮窗可自定义滑动
            FloatUtils.setFloatViewCanMove(windowManager,bootTextView){
                val pos = IntArray(2)
                bootTextView.getLocationOnScreen(pos)
                keyInfoTextView.text = "X: ${pos[0] + size / 2}, Y: ${pos[1] + size / 2}"
            }
        }
        // 设置提交事件
        saveConfView.onClick {
            val pointArr = Array(keyBootNumber){Point()}
            // 遍历控件获取位置并从窗口管理器中移除
            views.indices.forEach {
                val pos = IntArray(2)
                views[it]!!.getLocationOnScreen(pos)
                pointArr[it].run {
                    x = pos[0].toDouble() + size / 2
                    y = pos[1].toDouble() + size / 2
                }
                windowManager.removeView(views[it]!!)
            }
            keyBootComplete?.invoke(pointArr)
            CommonUtils.showDesktopToast(mContext,"键位设置成功!")
            FloatWindow.destroy(BootFloatWinName)
            hasRunning = false
        }
    }

}